/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package domain;
import domain.exceptions.YouCannotKillMeYouBusteredException;
import domain.exceptions.MultipleAgentsHaveTheSameLocation;
import domain.exceptions.TryingToRemoveNonExistingFlagException;
import domain.exceptions.CannotMoveThereException;
import domain.exceptions.NoSuchSquareException;
import domain.Game.direction;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author shaigi
 */
public class AgentDumbAuto extends Agent{
    
    
    public AgentDumbAuto(String name,direction lastDirection) {
        super(name, lastDirection);
    }
    
    @Override
    public void die() {
        System.err.println(this.toString() + "says: Dutz");
    }

    @Override
    public void play() {
        System.out.println("play");     //TODO del
        // search for an obstacle in the destination square:
        if (tryToMakeMove()){
        } else {
            changeDirection();
            tryToMakeMove();
        }
        shoot();
    }

    /**
     * 
     * @param agent to shoot
     * @return true iff there is a clean shot between this and the agent
     */
    private boolean inSight(Agent agent) {
        try {
        	//using inSight in the board, that gets as a parameter two squares and return
        	//true iff there is a they are parallel and have no obsticle between them.
            return Game._board.inSight(this.getSquare(), agent.getSquare());
        } catch (MultipleAgentsHaveTheSameLocation ex) {
            Logger.getLogger(AgentDumbAuto.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;//TODO: CHECK IF NOT TO CHANGE
    }

    private Vector<Agent> scan() {

        Vector<Agent> agents=Game.getAgents();
        Vector<Agent> agentsToKill=new Vector<Agent>();
        for (Iterator<Agent> it = agents.iterator(); it.hasNext();) {
            Agent agent = it.next();
            if( agent != this && inSight(agent) ){
                agentsToKill.add(agent);
            }
        }//end of for
        return agentsToKill;
    }
    
    private boolean tryToMakeMove(){
        direction lastDirection = getLastDirection();
        Square nextSquare = null;
        try {
            nextSquare = getSquare().neighbour(lastDirection);
        } catch (NoSuchSquareException ex) {
            System.out.println(ex.getMessage());
        }
        if( !( nextSquare instanceof SquareWall || nextSquare.isAgentHere())){
            try {
                move(lastDirection);
            } catch (CannotMoveThereException ex) {
                System.out.println(ex.getMessage());
                return false;
            }
            if (nextSquare.isContainFlag()){
                try {
                    takeFlag(nextSquare);
                } catch (TryingToRemoveNonExistingFlagException ex) {
                    System.err.println("Flag doesn't exist here: " + getSquare() );
                }
            }
            return true;
        }
        return false;
    }

    private void changeDirection() {
        if (getLastDirection() == direction.RIGHT) {
            setLastDirection(direction.LEFT);
        } else {
            setLastDirection(direction.RIGHT);
        }
    }

    private void shoot(){
        Vector<Agent> agentsToShoot=scan();
        for (Iterator<Agent> it = agentsToShoot.iterator(); it.hasNext();) {
            it.next().die();//TODO: check here if we do not miss the first agent.
        }

    }
    
    
 
}
